package com.jsu.user.controller.v1;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.jsu.user.service.impl.WebLoginServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.model.common.dtos.ResponseResult;
import org.example.model.common.enums.RedisCodeEnum;
import org.example.model.common.enums.WebHttpCodeEnum;
import org.example.model.user.dto.ForgetDto;
import org.example.model.user.dto.LoginDto;
import org.example.model.user.dto.RegisterDto;
import org.example.utils.common.JwtUtil;
import org.example.utils.common.WebJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user/login/web/v1")
@Api(tags = "登录控制器")
public class WebLoginController {
    @Autowired
    private WebLoginServiceImpl webLoginService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired(required = false)
    private WebJwtUtil jwtUtil;

    @ApiOperation(value = "token刷新",tags = "token刷新")
    @PostMapping("/refresh")
    public Map refresh(HttpServletRequest request){
        String authorization = request.getHeader("token-long");
        return jwtUtil.refresh(authorization);
    }


    @GetMapping("/forget/{phone}")
    @ApiOperation(value = "忘记密码发送验证码")
    public ResponseResult forgetPassword(@PathVariable String phone) {
        return webLoginService.forgetPassword(phone);
    }

    @PostMapping("/forget/password")
    @ApiOperation(value = "忘记密码")
    public ResponseResult forgetPassword(@RequestBody ForgetDto forgetDto) {
        return webLoginService.changePassword(forgetDto);
    }


    @ApiOperation(value = "登录")
    @PostMapping("/")
    public ResponseResult login(@RequestBody LoginDto loginDto){
        return webLoginService.loginUser(loginDto);
    }

    @ApiOperation(value = "注册时发送验证码")
    @PostMapping("/code/{phone}")
    public ResponseResult registerCode(@PathVariable String phone){
        log.info("开始发送验证");
        return webLoginService.registerEmail(phone);
    }

    @ApiOperation(value = "注册新账号")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody RegisterDto registerDto){
        return webLoginService.register(registerDto);
    }

    @ApiOperation(value = "创建验证码")
    @GetMapping("/get/code")
    public ResponseResult createCaptcha(HttpServletResponse response) throws IOException {
        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        // 图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        // 关闭流
        response.getOutputStream().close();
        return ResponseResult.okResult(lineCaptcha.getCode());
    }


}
